package com.ruoyi.business.service.koc.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.dto.admin.koc.TbUserKocProductLogPageDTO;
import com.ruoyi.business.domain.koc.*;
import com.ruoyi.business.domain.vo.TbUserKocProductLogPageVO;
import com.ruoyi.business.domain.vo.TbUserKocTaskInfoDetailVO;
import com.ruoyi.business.domain.vo.TbUserKocTaskInfoUserDetailVO;
import com.ruoyi.business.domain.vo.TraceabilityDetailVO;
import com.ruoyi.business.mapper.koc.TbUserKocProductLogMapper;
import com.ruoyi.business.service.koc.*;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * 用户获取产品流水Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-11
 */
@Service
public class TbUserKocProductLogServiceImpl extends ServiceImpl<TbUserKocProductLogMapper, TbUserKocProductLog> implements ITbUserKocProductLogService {


    @Resource
    private ITbUserKocTaskInfoDetailService tbUserKocTaskInfoDetailService;

    @Resource
    private ITbUserKocTaskInfoService tbUserKocTaskInfoService;

    @Resource
    private ITbKocTaskService tbKocTaskService;

    @Resource
    private ITbUserService tbUserService;


    @Override
    public TableDataInfo<TbUserKocProductLogPageVO> selectPageList(TbUserKocProductLogPageDTO tbUserKocProductLogPageDTO, PageQuery pageQuery) {
        Page<TbUserKocProductLogPageVO> page = pageQuery.build();
        IPage<TbUserKocProductLogPageVO> pageRes = baseMapper.selectPageList(page, tbUserKocProductLogPageDTO);
        return TableDataInfo.build(pageRes);
    }

    @Override
    public TraceabilityDetailVO selectDetail(Long sourceId) {
        TbUserKocTaskInfo tbUserKocTaskInfo = tbUserKocTaskInfoService.getOne(Wrappers.lambdaQuery(TbUserKocTaskInfo.class).eq(TbUserKocTaskInfo::getCheckBusinessId, sourceId).last(Constants.MYBATIS_PLUSH_LIMIT_ONE));
        TbUserKocTaskInfoUserDetailVO tbUserKocTaskInfoUserDetailVO = BeanUtil.toBean(tbUserKocTaskInfo, TbUserKocTaskInfoUserDetailVO.class);
        TbUser tbUser = tbUserService.getById(tbUserKocTaskInfo.getUserId());
        TbKocTask tbKocTask = tbKocTaskService.getById(tbUserKocTaskInfo.getKocTaskId());
        List<TbUserKocTaskInfoDetail> detailList = tbUserKocTaskInfoDetailService.list(Wrappers.lambdaQuery(TbUserKocTaskInfoDetail.class).eq(TbUserKocTaskInfoDetail::getKocTaskInfoId, tbUserKocTaskInfo.getId()));
        List<TbUserKocTaskInfoDetailVO> tbUserKocTaskInfoDetailVOS = BeanUtil.copyToList(detailList, TbUserKocTaskInfoDetailVO.class);
        tbUserKocTaskInfoUserDetailVO.setTbUserKocTaskInfoDetails(tbUserKocTaskInfoDetailVOS);
        return TraceabilityDetailVO.builder().tbKocTask(tbKocTask).tbUserKocTaskInfos(tbUserKocTaskInfoUserDetailVO).tbUser(tbUser).build();
    }

    @Override
    public TableDataInfo<TbUserKocProductLog> queryMyProductLogsPage(Long userId, Long checkBusinessId, String kocDate, PageQuery pageQuery) {
        LambdaQueryWrapper<TbUserKocProductLog> lambdaQueryWrapper = Wrappers.lambdaQuery(TbUserKocProductLog.class)
            .eq(TbUserKocProductLog::getUserId, userId)
            .eq(ObjectUtils.isNotEmpty(checkBusinessId), TbUserKocProductLog::getSourceId, checkBusinessId)
            .eq(StringUtils.isNotEmpty(kocDate), TbUserKocProductLog::getKocDate, kocDate);
        lambdaQueryWrapper.orderByDesc(TbUserKocProductLog::getCreateTime);
        Page<TbUserKocProductLog> page = baseMapper.selectPage(pageQuery.build(), lambdaQueryWrapper);
        return TableDataInfo.build(page);
    }



}
